import { observable, action } from 'mobx';
import { WeaTools } from 'ecCom';
import moment from 'moment';
import * as knowledgeMap from '../../apis/knowledgeMap/knowledgeMap'

let Base64 = ecCom.WeaTools.Base64
export default class KnowledgeEditStore {

  constructor(props) {
  }
  @observable loading = false;
  @observable hasRight = false;
  @observable name = "";
  @observable remark = "";
  @observable datas = [];
  @observable hasaddmenu = false;
  @observable mbtitle = "";
  @observable mbbg = "";
  @observable tId = "1";
  @observable maptype = "";
  @observable previewimg = "";
  @observable previewimgArray = [];
  @observable type = 0;
  @observable modellist = [];
  @observable maptypelist = [];
  @observable maptypeOption = [];

  @observable iframeSrc = "";
  /**
   * 获取知识点详细信息
   */
  @action
  getKnowledgeDetail(params) {
    this.loading = true;
    knowledgeMap.getKnowledgeDetail(params).then((data) => {
      if (data.status == 1) {
        this.name = data.name;
        this.remark = data.remark;
        this.hasRight = data.hasRight;
        this.datas = data.dataDetail;
        this.hasaddmenu = data.hasaddmenu;
        this.mbtitle = data.mbtitle;
        this.mbbg = data.mbbg;
        this.tId = data.tId;
        this.maptype = data.maptype;
        this.previewimg = data.previewimg;
        this.previewimgArray = data.previewimgArray;

        this.type = data.type;
        if (this.type == "1") {
          if (params.operation == "edit") {
            this.iframeSrc = "/knowledgeMap/base/MapModelType.jsp?mapId=" + params.knowledgeId;
          } else if (params.operation == "view") {
            this.iframeSrc = "/knowledgeMap/template/ShowFckEditorDesc1.jsp?requestid=1&mapId=" + params.knowledgeId;
          }
        }
        this.modellist = data.modellist;
        this.maptypelist = data.maptypelist;
        this.maptypeOption = this.getMapTypeOption(data.maptypelist);
      }
      this.loading = false;
    });
  }
  @action
  getshowModeList(params) {
    this.loading = true;
    knowledgeMap.getshowModeList(params).then((data) => {
      if (data.status == 1) {
        this.modellist = data.modellist;
        this.maptypelist = data.maptypelist;
        this.maptypeOption = this.getMapTypeOption(data.maptypelist);
      }
      this.loading = false;
    });
  }
  /**
   * 保存知识点信息
   */
  @action
  saveKnowledge(params) {
    this.loading = true;
    if (params.contents.length > 0) {
      params.contents = Base64.encode(params.contents);
    }
    knowledgeMap.operateKnowledge(params).then((data) => {
      if (data.status == 1) {
        let knowledgeId = data.knowledgeId || "";
        if (params.doback && knowledgeId) {
          params.doback(knowledgeId, data.updatedate, data.updateId, data.updaterName);
        }
      }
      this.loading = false;
    });
  }

  getMapTypeOption(maptypelist) {
    let optionArray = [];
    let mapOption = {};
    mapOption.key = "";
    mapOption.showname = "";
    optionArray.push(mapOption);
    for (let i = 0; i < maptypelist.length; i++) {
      mapOption = {};
      mapOption.key = maptypelist[i].id;
      mapOption.showname = maptypelist[i].name;
      optionArray.push(mapOption);
    }
    return optionArray;
  }

  /**
   * 保存知识点信息
   */
  @action
  saveMapType(params) {
    this.loading = true;
    knowledgeMap.saveMapType(params).then((data) => {
      if (data.status == 1) {
        this.maptypelist = data.listType;
        this.maptypeOption = this.getMapTypeOption(data.listType);
        if (params.doback) {
           params.doback();
        }
      }
      this.loading = false;
    });
  }
}